package com.zxy;
public class StringParser {
    enum State {
        START,
        INSIDE_STRING,
        ESCAPE_SEQUENCE
    }

    public static void main(String[] args) {
        String input = "一般学习一门编程语言的时候，基本上第一个程序是输出\"Hello World!\"";
        String result = parseString(input);
        System.out.println(result);
    }

    public static String parseString(String input) {
        State currentState = State.START;
        StringBuilder output = new StringBuilder();

        for (int i = 0; i < input.length(); i++) {
            char c = input.charAt(i);
            switch (currentState) {
                case START:
                    if (c == '"') {
                        currentState = State.INSIDE_STRING;
                        output.append(c); // 添加双引号到输出中
                    } else {
                        output.append(c); // 添加非字符串部分到输出中
                    }
                    break;
                case INSIDE_STRING:
                    if (c == '\\') {
                        currentState = State.ESCAPE_SEQUENCE;
                    } else if (c == '"') {
                        currentState = State.START;
                        output.append(c); // 添加闭合的双引号到输出中
                    } else {
                        output.append(c); // 添加字符串内容到输出中
                    }
                    break;
                case ESCAPE_SEQUENCE:
                    switch (c) {
                        case 'n':
                            output.append('\n');
                            break;
                        case 't':
                            output.append('\t');
                            break;
                        case '"':
                            output.append('"');
                            break;
                        case '\\':
                            output.append('\\');
                            break;
                        default:
                            System.out.println("Invalid escape sequence at position " + i);
                            output.append(c); // 如果是非法转义序列，直接添加字符
                    }
                    currentState = State.INSIDE_STRING;
                    break;
            }
        }
        return output.toString();
    }
}